package cn.edu.cqupt.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.net.ssl.SSLServerSocket;

import org.springframework.stereotype.Service;

import cn.edu.cqupt.dao.ActivityMapper;
import cn.edu.cqupt.dao.AdmirerecordMapper;
import cn.edu.cqupt.dao.CUserMapper;
import cn.edu.cqupt.dao.ClassachievementMapper;
import cn.edu.cqupt.dao.ClassnewsMapper;
import cn.edu.cqupt.dao.CommentsMapper;
import cn.edu.cqupt.dao.HeartwordsMapper;
import cn.edu.cqupt.dao.MemberMapper;
import cn.edu.cqupt.dao.PersonachievementMapper;
import cn.edu.cqupt.core.JobsConstants;
import cn.edu.cqupt.core.PageParam;
import cn.edu.cqupt.model.Activity;
import cn.edu.cqupt.model.Admirerecord;
import cn.edu.cqupt.model.CUser;
import cn.edu.cqupt.model.Classachievement;
import cn.edu.cqupt.model.Classnews;
import cn.edu.cqupt.model.Comments;
import cn.edu.cqupt.model.CommentsPage;
import cn.edu.cqupt.model.HeartwordPage;
import cn.edu.cqupt.model.Heartwords;
import cn.edu.cqupt.model.Member;
import cn.edu.cqupt.model.Personachievement;
import cn.edu.cqupt.service.HomeService;

@Service("homeService")
public class HomeServiceImpl implements HomeService {
	@Resource
	private MemberMapper memberMapper;
	@Resource
	private CUserMapper cUserMapper;
	@Resource
	private PersonachievementMapper personachievementMapper;
	@Resource
	private ClassachievementMapper classachievementMapper;
	@Resource
	private ClassnewsMapper classnewsMapper;
	@Resource
	private ActivityMapper activityMapper;
	@Resource
	private HeartwordsMapper heartwordsMapper;
	@Resource
	private CommentsMapper commentsMapper;
	@Resource
	private AdmirerecordMapper admirerecordMapper;
	
	
	public ClassnewsMapper getClassnewsMapper() {
		return classnewsMapper;
	}


	public void setClassnewsMapper(ClassnewsMapper classnewsMapper) {
		this.classnewsMapper = classnewsMapper;
	}


	public ClassachievementMapper getClassachievementMapper() {
		return classachievementMapper;
	}


	public void setClassachievementMapper(ClassachievementMapper classachievementMapper) {
		this.classachievementMapper = classachievementMapper;
	}


	public PersonachievementMapper getPersonachievementMapper() {
		return personachievementMapper;
	}


	public void setPersonachievementMapper(PersonachievementMapper personachievementMapper) {
		this.personachievementMapper = personachievementMapper;
	}


	public CUserMapper getcUserMapper() {
		return cUserMapper;
	}


	public void setcUserMapper(CUserMapper cUserMapper) {
		this.cUserMapper = cUserMapper;
	}


	public MemberMapper getMemberMapper() {
		return memberMapper;
	}


	public void setMemberMapper(MemberMapper memberMapper) {
		this.memberMapper = memberMapper;
	}


	@Override
	public HashMap<String, Object> selectmemberByuserid(String username, String password) throws Exception {
		//根据用户名判断用户是否存在
		CUser user=cUserMapper.isHaveUser(username);
		if(user==null){
				throw new Exception("该用户不存在");
		}
		//查询用户状态是否合法，1为有效状态
		String status=user.getUserstatus().toLowerCase();
		if (!JobsConstants.LOGIN_USER_STATUS_NORMAL.equals(status)) {
			switch (status) {
			case JobsConstants.LOGIN_USER_STATUS_FREEZE:
				throw new Exception("该用户账号已被冻结");
			default:
				throw new Exception("该用户账号存在异常");
			}
		}
		
		//检查用户名和密码是否匹配
		if(!password.equals(user.getPassword())){
			throw new Exception("账号或密码不对");
		}
		//用户信息正常，返回登录用户的信息
		Member member=memberMapper.selectMemberByuserid(user.getUserid());
		HashMap<String, Object> loginuser=new HashMap<>();
		loginuser.put("user", user);
		loginuser.put("member", member);
		return loginuser;
	}


	@Override
	public int updatememberselective(Member member) {
		return memberMapper.updateByPrimaryKeySelective(member);
	}


	@Override
	public int selectcountPachievements() {
		return personachievementMapper.selectcountPachievements();
	}


	@Override
	public PageParam<Personachievement> selectallPachievements(PageParam<Personachievement> pageParam) {
		int offset  = (pageParam.getCurrPage() -1) * pageParam.getPageSize();
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", offset);
		params.put("size", pageParam.getPageSize());
		List<Personachievement> pachieves=personachievementMapper.selectallPachievements(params);
		pageParam.setData(pachieves);
		return pageParam;
	}
	
	@Override
	public PageParam<Classachievement> selectallCachievements(PageParam<Classachievement> pageParam) {
		int offset  = (pageParam.getCurrPage() -1) * pageParam.getPageSize();
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", offset);
		params.put("size", pageParam.getPageSize());
		List<Classachievement> ca=classachievementMapper.selectallCachievements(params);
		pageParam.setData(ca);
		return pageParam;
	}


	@Override
	public int deletePachieve(String id) {
		return personachievementMapper.deleteByPrimaryKey(id);
	}


	@Override
	public int insertPachieve(Personachievement pa) {
		return personachievementMapper.insertSelective(pa);
	}


	@Override
	public int updatePachieve(Personachievement pa) {
		return personachievementMapper.updateByPrimaryKeySelective(pa);
	}


	@Override
	public int selectcountCachievements() {
		
		return classachievementMapper.selectachievecount();
	}


	@Override
	public int deleteCachieve(String id) {
		
		return classachievementMapper.deleteByPrimaryKey(id);
	}


	@Override
	public int insertCachieve(Classachievement ca) {
		
		return classachievementMapper.insertSelective(ca);
	}


	@Override
	public int updateCachieve(Classachievement ca) {
	
		return classachievementMapper.updateByPrimaryKeySelective(ca);
	}


	@Override
	public List<Classachievement> selectnewClassachieves() {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", 0);
		params.put("size",3);
		List<Classachievement> ca=classachievementMapper.selectallCachievements(params);
		return ca;
	}


	@Override
	public int updatepassword(CUser user) {
		return cUserMapper.updateByPrimaryKeySelective(user);
	}


	@Override
	public List<Classnews> selectNews() {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", 0);
		params.put("size",4);
		List<Classnews> news=classnewsMapper.selectNewsBypage(params);
		return news;
	}


	@Override
	public Classnews selectNewsByid(String newid) {
		
		return classnewsMapper.selectByPrimaryKey(newid);
	}


	@Override
	public int selectNewsCount() {
		
		return classnewsMapper.selectNewsCount();
	}


	@Override
	public PageParam<Classnews> selectallNewsBypage(PageParam<Classnews> pageParam) {
		int offset  = (pageParam.getCurrPage() -1) * pageParam.getPageSize();
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", offset);
		params.put("size", pageParam.getPageSize());
		List<Classnews>news=classnewsMapper.selectNewsBypage(params);
		pageParam.setData(news);
		  return pageParam;
	}


	@Override
	public int insertNews(Classnews news) {
		return classnewsMapper.insertSelective(news);
	}


	@Override
	public int deleteNews(String newid) {
		return classnewsMapper.deleteByPrimaryKey(newid);
	}


	@Override
	public List<Activity> selectActivitiesBypage() {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", 0);
		params.put("size",5);
		return activityMapper.selectActivitiesBypage(params);
	}


	@Override
	public int selectActivityCount() {
		
		return activityMapper.selectActivityCount();
	}


	@Override
	public PageParam<Activity> selectallActivityBypage(PageParam<Activity> pageParam) {
		int offset  = (pageParam.getCurrPage() -1) * pageParam.getPageSize();
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", offset);
		params.put("size", pageParam.getPageSize());
		List<Activity>acs=activityMapper.selectActivitiesBypage(params);
		pageParam.setData(acs);
		return pageParam;
	}


	@Override
	public int deleteAcitvityByid(String id) {
		
		return activityMapper.deleteByPrimaryKey(id);
	}


	@Override
	public int insertActivity(Activity ac) {
		
		return activityMapper.insertSelective(ac);
	}


	@Override
	public Activity selectActivity(String activityid) {
		
		return activityMapper.selectByPrimaryKey(activityid);
	}


	@Override
	public int insertHeartword(Heartwords word) {
		return heartwordsMapper.insertSelective(word);
	}


	@Override
	public List<HeartwordPage> selectPopularWords() {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", 0);
		params.put("size",5);
		//获得点赞量最大的五条留言
		List<Heartwords> words=heartwordsMapper.selectPopularHeartWordsBypage(params);
		//获取每条留言的评论信息，并将这些信息封装成页面需要的信息
		List<HeartwordPage> list= new ArrayList<HeartwordPage>();
		SimpleDateFormat bartDateFormat=new SimpleDateFormat("yy-MM-dd");//定制页面时间格式
		if (words!=null){
		 for(Heartwords word:words){
			 HeartwordPage page=new HeartwordPage();
			 page.setClicknum(word.getClicknum());
			 page.setCreatetime(bartDateFormat.format(word.getCreatetime()));
			 page.setHeartword(word.getHeartword());
			 page.setId(word.getId());
			 page.setUserid(word.getUserid());
			 //获得留言用户的基本信息
			 Member mem=memberMapper.selectMemberByuserid(word.getUserid());
			 page.setMembername(mem.getMembername());
			 page.setPhoto(mem.getPhoto());
			 //获得该条留言的所有评论信息
			 List<Comments> coments=commentsMapper.selectallCommentsByheartwordid(word.getId());
			 //将信息封装成页面数据信息
			 if (coments!=null){//该条留言有用户评论
			 List<CommentsPage>cps=new ArrayList<CommentsPage>();
			 for (Comments cp:coments){//将所有的评论信息封装成页面用户查看的信息
				 CommentsPage cpe=new CommentsPage();
				 Member member=memberMapper.selectMemberByuserid(cp.getUserid());
				 cpe.setComment(cp.getComment());
				 cpe.setCommentid(cp.getCommentid());
				 cpe.setCreatetime(bartDateFormat.format(cp.getCreatetime()));
				 cpe.setMembername(member.getMembername());
				 cpe.setPhoto(member.getPhoto());
				 cpe.setUserid(member.getUserid());
				 cps.add(cpe);	 
			 }
			 page.setList(cps); 
			 }
			 list.add(page);
		 }
		}
		return list;
	}

	@Override
	public int selectHeartWordsCount() {
		return heartwordsMapper.selectHeartWordsCount();
	}


	@Override
	public PageParam<HeartwordPage> selectHeartwordsBypage(PageParam<HeartwordPage> pageParam,String userid) {
		int offset  = (pageParam.getCurrPage() -1) * pageParam.getPageSize();
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", offset);
		params.put("size", pageParam.getPageSize());
		List<Heartwords> words=heartwordsMapper.selectHeartwordsTopage(params);
		//获取每条留言的评论信息，并将这些信息封装成页面需要的信息
		List<HeartwordPage> list= new ArrayList<HeartwordPage>();
		if (words!=null){
				SimpleDateFormat bartDateFormat=new SimpleDateFormat("yy-MM-dd");//定制页面时间格式
				 for(Heartwords word:words){
					 HeartwordPage page=new HeartwordPage();
					 //结合点赞记录表，判断当前用户此条说说是否可以点赞
					 if(userid.equals("null")){
						 //当前用户是游客，不能点赞
						page.setAdmirestatus("0"); 
					 }
					 else {
						 //根据userid获取该用户点赞的心情id
						 List<String> wordids=new ArrayList<>();
						 wordids=admirerecordMapper.selectallwordidByuserid(userid);
						 if (wordids==null){
							 page.setAdmirestatus("1");
						 }
						 else if (wordids.contains(word.getId()))
							 page.setAdmirestatus("0");
						 else page.setAdmirestatus("1");
					 }
					 page.setClicknum(word.getClicknum());
					 page.setCreatetime(bartDateFormat.format(word.getCreatetime()));
					 page.setHeartword(word.getHeartword());
					 page.setId(word.getId());
					 page.setUserid(word.getUserid());
					 //获得留言用户的基本信息
					 Member mem=memberMapper.selectMemberByuserid(word.getUserid());
					 page.setMembername(mem.getMembername());
					 page.setPhoto(mem.getPhoto());
					 //获得该条留言的所有评论信息
					 List<Comments> coments=commentsMapper.selectallCommentsByheartwordid(word.getId());
					 //将信息封装成页面数据信息
					 if (coments!=null){//该条留言有用户评论
					 List<CommentsPage>cps=new ArrayList<CommentsPage>();
					 for (Comments cp:coments){//将所有的评论信息封装成页面用户查看的信息
						 CommentsPage cpe=new CommentsPage();
						 Member member=memberMapper.selectMemberByuserid(cp.getUserid());
						 cpe.setComment(cp.getComment());
						 cpe.setCommentid(cp.getCommentid());
						 cpe.setCreatetime(bartDateFormat.format(cp.getCreatetime()));
						 cpe.setMembername(member.getMembername());
						 cpe.setPhoto(member.getPhoto());
						 cpe.setUserid(member.getUserid());
						 cps.add(cpe);	 
					 }
					 page.setList(cps); 
					 }
					 list.add(page);
				 }
				 
		}
		 pageParam.setData(list);
		return pageParam;
	}


	@Override
	public int updateHeartWordClicknums(String id) {
		return heartwordsMapper.updateHeartWordClicknums(id);
	}


	@Override
	public int insertComment(Comments com) {
		
		return commentsMapper.insertSelective(com);
	}


	@Override
	public int deleteComments(String commentid) {
		return commentsMapper.deleteByPrimaryKey(commentid);
	}


	@Override
	public int deleteWordAndComments(String heartwordid) {
		int res=0;
		try {
			int res1=heartwordsMapper.deleteByPrimaryKey(heartwordid);
			int res2=commentsMapper.deleteByheartwordid(heartwordid);
			res=1;
		} catch (Exception e) {
			throw new RuntimeException("删除心情及评论出错了！！",e);
		}
		return res;
	}


	@Override
	public String selectClicknumByid(String id) {
	
		return heartwordsMapper.selectClicknumByid(id);
	}


	@Override
	public int updateClicknums(Admirerecord record) {
		int res=0;
		try {
			int res1=heartwordsMapper.updateHeartWordClicknums(record.getHeartwordid());
			int res2=admirerecordMapper.insertSelective(record);
			res=1;
		} catch (Exception e) {
			throw new RuntimeException("点赞失败",e);
		}
		return res;
	}


	@Override
	public List<Admirerecord> selectRecordByheartid(String id) {
		
		return admirerecordMapper.selectRecordByheartid(id);
	}


	@Override
	public List<Member> selectallMembers() {
		return memberMapper.selectallMembers();
	}


	@Override
	public Member selectMemberInfoByid(String memberid) {
		
		return memberMapper.selectByPrimaryKey(memberid);
	}


	@Override
	public int selectRowCountBypersonname(String personname) {
		
		return personachievementMapper.selectRowCountBypersonname(personname);
	}


	@Override
	public PageParam<Personachievement> selectPByname(PageParam<Personachievement> pageParam, String personname) {
		int offset  = (pageParam.getCurrPage() -1) * pageParam.getPageSize();
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("offset", offset);
		params.put("size", pageParam.getPageSize());
		params.put("personname", personname);
		List<Personachievement> data=personachievementMapper.selectPByname(params);
		pageParam.setData(data);
		return pageParam;
	}


	@Override
	public List<String> selectallPersonnames() {
	
		return personachievementMapper.selectallPersonnames();
	}


}
